<?php

Class Mod_sale_management extends Model {

    function Mod_sale_management() {
        parent::Model();
    }
    

//===========this is function add card=================//

    function add_card() {
        $get_card = $this->input->post('card');
        $get_car_quan = $this->input->post('txt_quantity');
        $query_card = $this->db->query('select *from tbl_card');
        foreach ( $query_card->result_array() as $row_card) {
            $car_quan = $row_card['car_quantity'];
          
            $cardid=$row_card['car_id'];
          //  echo   $cardid;die();
            if( $cardid==$get_card){
                 if( $car_quan < $get_car_quan){
                        $this->session->set_userdata('invalid_sal','<p  style="text-align:center; color:red;">product is not enough, please check product in stock!</p>');
                       redirect('sale_management/add_card');
                    }else{
                  $re_quantity = (( $car_quan) - ( $get_car_quan));
                 
            }
        }
        }
        
        $seg_user=$this->session->userdata('use_username');  //====get session from user login=======//
        
      //  echo $seg_user;die();
        $data = array(
            'stu_id' => $this->input->post('student'),
            'car_id' => $this->input->post('card'),
            'sal_status' => 1,
            'sal_quantity' => $this->input->post('txt_quantity'),
            'sal_date' => $this->input->post('txt_saleDate'),
            'sal_price' => $this->input->post('txt_price'),
            'sal_payment' => $this->input->post('txt_payment'),
            'use_username'=>  $seg_user
        );
        
       $this->db->insert('tbl_sale', $data);  //====query data from tbl_sale in order to compare======//
        $data_updat = array(
            'car_quantity' => $re_quantity,
        );
        $this->db->where('car_id', $get_card);
        return $this->db->update('tbl_card', $data_updat); //======update data into table card when sale product=========//
    }

    //==============this is function adding material===================//


    function add_material() {
        $get_pro = $this->input->post('txt_proname');
      
        $get_pro_quan = $this->input->post('txt_quantity');

        $query_pro = $this->db->query('select *from tbl_product');
        foreach ($query_pro->result_array() as $row_pro) {
            $pro_quan = $row_pro['pro_quantity'];
            $proid=$row_pro['pro_id'];
            
            if( $proid==$get_pro){
                    if($pro_quan < $get_pro_quan){
                        $this->session->set_userdata('invalid_sal','<p  style="text-align:center; color:red;">product is not enough, please check product in stock!</p>');
                       redirect('sale_management/add_material');
                    }else{
                  $re_quantity = (($pro_quan) - ($get_pro_quan));
                    }
            }  
            }
            
        $seg_user=$this->session->userdata('use_username'); //====get session from user login=======//
        
        $data = array(
            'sal_price' => $this->input->post('txt_price'),
            'stu_id' => $this->input->post('customer_name'),
            'sal_date' => $this->input->post('txt_saleDate'),
            'stu_type' => $this->input->post('txt_studtype'),
          //  'use_id' => $this->input->post('txt_seller'),
            //    'use_id' =>$this->session->userdata('userid'),
            'pro_id' => $this->input->post('txt_proname'),
            'sal_quantity' => $this->input->post('txt_quantity'),
            'sal_description' => $this->input->post('txt_desc'),
            'sal_status' => 1,
            'use_username'=>$seg_user
        );
         $this->db->insert('tbl_sale', $data);
        $data_updat = array(
            'pro_quantity' => $re_quantity,
        );
        $this->db->where('pro_id', $get_pro);
        return $this->db->update('tbl_product', $data_updat);
    }

    //================this is function for select edit material=====================//
    
    function select_edit() {
        $se_id = $this->uri->segment(3);
        $this->db->select('*')->from('tbl_sale')->where('sal_id', $se_id);
        // $this->db->from('tbl_sale');
        //$this->db->where('sal_id',$se_id);
        return $this->db->get();
    }

    //=========================this is function using check edit material===================//

    function check_edit() {
        $data = array(
            'sal_price' => $this->input->post('txt_price'),
            'stu_id' => $this->input->post('customer_name'),
            'sal_date' => $this->input->post('txt_saleDate'),
            'stu_type' => $this->input->post('txt_studtype'),
           // 'use_id' => $this->input->post('txt_seller'),
            'pro_id' => $this->input->post('txt_proname'),
            'sal_quantity' => $this->input->post('txt_qauntity'),
            'sal_description' => $this->input->post('txt_desc'),
        );
        $seg_update = $this->uri->segment(3);
        $this->db->where('sal_id', $seg_update);
        return $this->db->update('tbl_sale', $data);
    }
    //============View detail of Material product===============//

    function view_detail_material() {
        $se_id = $this->uri->segment(3);
        $this->db->where('sal_id', $se_id);
        $this->db->select('*');
        $this->db->from('tbl_sale');
        $this->db->join('tbl_student', 'tbl_student.stu_id=tbl_sale.stu_id');
      //  $this->db->join('tbl_user', 'tbl_user.use_id=tbl_sale.use_id');
        $this->db->join('tbl_product', 'tbl_product.pro_id=tbl_sale.pro_id');
        return $this->db->get();
    }
    
    //===================View detail of Material product================//

    function view_detail_card() {
        
        $se_id = $this->uri->segment('3');
        $this->db->where('sal_id', $se_id);
        $this->db->select('*');
        $this->db->from('tbl_sale');
        $this->db->join('tbl_card', 'tbl_card.car_id=tbl_sale.car_id');
        $this->db->join('tbl_pricecard', 'tbl_card.car_id=tbl_pricecard.car_id');
        $this->db->join('tbl_car_type', 'tbl_car_type.car_type_id=tbl_card.car_type_id');
        $this->db->join('tbl_student', 'tbl_student.stu_id=tbl_sale.stu_id');
        $this->db->join('tbl_stu_type', 'tbl_stu_type.stu_type_id=tbl_student.stu_type_id');
 
        return $this->db->get();
    }

    //=====================this is function for edit sale of card=================//

    function card_edit() {
        $data = array(
            'stu_id' => $this->input->post('student'),
            'car_id' => $this->input->post('card'),
            'sal_status' => 1,
            'sal_quantity' => $this->input->post('txt_quantity'),
            'sal_date' => $this->input->post('txt_saleDate'),
            'sal_price' => $this->input->post('txt_price'),
            'sal_payment' => $this->input->post('txt_payment'),
            'sal_description'=>$this->input->post('txt_description')
        );
        $seg = $this->uri->segment(3);
        $this->db->where('sal_id', $seg);
        return $this->db->update('tbl_sale', $data);
    }

    //==============This function is used to select in order to edit card sale=============//


   function editcardsale() {
        $se_id = $this->uri->segment('3');
        $this->db->where('sal_id', $se_id);
        $this->db->select('*');
        $this->db->from('tbl_sale');
        $this->db->join('tbl_card', 'tbl_card.car_id=tbl_sale.car_id');
        $this->db->join('tbl_pricecard', 'tbl_card.car_id=tbl_pricecard.car_id');
        $this->db->join('tbl_car_type', 'tbl_car_type.car_type_id=tbl_card.car_type_id');
        $this->db->join('tbl_student', 'tbl_student.stu_id=tbl_sale.stu_id');
        $this->db->join('tbl_stu_type', 'tbl_stu_type.stu_type_id=tbl_student.stu_type_id');
        return $this->db->get();
    }

    //===============this is function for delete material==================//

    function delete_material() {
        $seg = $this->uri->segment('3');
        $this->db->where('sal_id', $seg);
        return $this->db->delete('tbl_sale');
    }

//===================delete material=================//

    function delete() {
        $seg_material = $this->uri->segment('3');
        $this->db->where('sal_id', $seg_material);
        return $this->db->delete('tbl_sale');
    }

    //===============delete card==================//

    function delete_card() {
        $seg_card = $this->uri->segment('3');
        $this->db->where('sal_id', $seg_card);
        return $this->db->delete('tbl_sale');
    }

    //=================this is pagination for sale material=============//


    function get_saleproduct($p=null) { // $p is array
        if ($this->input->post($this->dbf->pro_name) && $this->input->post($this->dbf->pro_name) != '')
            $this->db->like($this->dbf->pro_name, $this->input->post($this->dbf->pro_name));

        $this->db->limit($p[0], $p[1]);
        $this->db->where('sal_status', 1);
        $this->db->order_by('sal_id', 'desc');
        $this->db->select('*');
        $this->db->from('tbl_sale');
        $this->db->join('tbl_student', 'tbl_student.stu_id=tbl_sale.stu_id');
      //  $this->db->join('tbl_user', 'tbl_user.use_id=tbl_sale.use_id');
        $this->db->join('tbl_product', 'tbl_product.pro_id=tbl_sale.pro_id');

        return $this->db->get();
    }
    
    //==================searching card==================//
    
    function get_salecard($p=null) { // $p is array 
        if ($this->input->post($this->dbf->car_name) && $this->input->post($this->dbf->car_name) != '')
            $this->db->like($this->dbf->car_name, $this->input->post($this->dbf->car_name));

        $this->db->limit($p[0], $p[1]);
        $this->db->where('sal_status', 1);
        $this->db->order_by('sal_id', 'desc');
        $this->db->select('*');
                    $this->db->from('tbl_sale');
                    $this->db->join('tbl_card', 'tbl_card.car_id=tbl_sale.car_id');
                    $this->db->join('tbl_pricecard', 'tbl_card.car_id=tbl_pricecard.car_id');
                    $this->db->join('tbl_car_type', 'tbl_car_type.car_type_id=tbl_card.car_type_id');
                    $this->db->join('tbl_student', 'tbl_student.stu_id=tbl_sale.stu_id');
                    $this->db->join('tbl_stu_type', 'tbl_stu_type.stu_type_id=tbl_student.stu_type_id');
                  

        return $this->db->get();
    }
    
 //=====================this is the end of function==============//
    
}